package date;

public class GameRule {
    public static boolean isConnect(int[][] data, int row1, int col1, int row2, int col2) {
        if (col1 == col2 && data[row1][col1] == data[row2][col2]) {
            int col = col1;
            int sum = 0;
            if (row1 > row2) {
                int t = row1;
                row1 = row2;
                row2 = t;

            }
            for (int row = row1 + 1; row < row2; row++) {

                sum += data[row][col];
                if (sum == 0) {
                    return true;
                }
            }
        }
        if (col1 == col2 && data[row1][col1] == data[row2][col2]) {
            int col = col1;
            int sum = 0;
            if (row1 > row2) {
                int t = row1;
                row1 = row2;
                row2 = t;

            }
            for (int row = row1 + 1; row < row2; row++) {
                sum += data[row][col];
                if (sum == 0) {
                    return true;
                }
            }
        }

        if (row1 == row2 && col1 == col2) {
            System.out.println("点击同一个按钮，不能相连");
            return false;
        }
        if (data[row1][col1] != data[row2][col2]) {
            System.out.println("点击的不是相同的图案，不能相连");
            return false;
        }
        if(connect0(data , row1, col1, row2, col2)){
            System.out.println("直线相连");
            return true;
        }
        if(connect1(data, row1, col1, row2, col2)){
            System.out.println("一拐角相连");
            return true;
        }
        if(connect2(data, row1, col1, row2, col2)){
            System.out.println("二拐角相连");
            return true;
        }

        return false;

    }



    public static boolean connect0(int[][] data, int row1, int col1, int row2, int col2) {
        if (col1 == col2) {
            System.out.println("同一列");
            if (row1 > row2) {
                int t = row1;
                row1 = row2;
                row2 = t;
            }
for (int row = row1 + 1; row < row2; row++) {
    if(data[row][col1]!=0){
        return false;
    }

}
    return true;

        }
        if (row1 == row2) {
            System.out.println("同一行");
            if (col1 > col2) {
                int t = col1;
               col1 = col2;
                col2 = t;
            }
            for (int col = col1 + 1; col < col2; col++) {
                if(data[row1][col]!=0){
                    return false;
                }

            }
                return true;

        }
        return false;
    }
    //拐角
    private static boolean connect1(int[][] data, int row1, int col1, int row2, int col2) {
        if(connect0(data, row1, col1, row1, col2)&&connect0(data, row1, col2, row2, col2)&&data[row1][col2]==0) {
            System.out.println("拐角");
            return true;
        }
        if(connect0(data, row1, col1, row2, col1)&&connect0(data, row2, col1, row2, col2)&&data[row2][col1]==0) {
            System.out.println("拐角");
            return true;
        }
        return false;
    }
    // 二拐角
    private static boolean connect2(int[][] data, int row1, int col1, int row2, int col2) {
        int rows = data.length;
        int cols = data[0].length;

        // 水平方向找双拐角
        for (int col = 0; col < cols; col++) {
            if (col != col1 && col != col2) {
                if (connect0(data, row1, col1, row1, col) &&
                        connect0(data, row1, col, row2, col) &&
                        connect0(data, row2, col, row2, col2) &&
                        data[row1][col] == 0 &&
                        data[row2][col] == 0) {
                    return true;
                }
            }
        }

        // 垂直方向找双拐角
        for (int row = 0; row < rows; row++) {
            if (row != row1 && row != row2) {
                if (connect0(data, row1, col1, row, col1) &&
                        connect0(data, row, col1, row, col2) &&
                        connect0(data, row, col2, row2, col2) &&
                        data[row][col1] == 0 &&
                        data[row][col2] == 0) {
                    return true;
                }
            }
        }

        return false;
    }
}

